package com.jmu.medicineknows.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmu.medicineknows.dto.RoleMenuDto;
import com.jmu.medicineknows.dto.RoleQueryDto;
import com.jmu.medicineknows.entity.Menu;
import com.jmu.medicineknows.entity.Result;
import com.jmu.medicineknows.entity.Role;
import com.jmu.medicineknows.entity.RoleMenu;
import com.jmu.medicineknows.mapper.MenuMapper;
import com.jmu.medicineknows.mapper.RoleMapper;
import com.jmu.medicineknows.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: JieZi
 * @Date: 2023/7/19 19:38
 * @Description:
 **/
@Service
@SuppressWarnings("all")
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    @Override
    public Result list(RoleQueryDto dto) {
        IPage<Role> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.name != null) {
            queryWrapper.like(Role::getName, dto.getName());
        }
        IPage<Role> pageList = roleMapper.selectPage(page, queryWrapper);
        return Result.defaultSuccess(pageList);
    }

    @Override
    public Result update(Role role) {
        if (role.id == null) {
            role.setCreateTime(sdf.format(new Date()));
            return Result.defaultSuccess(roleMapper.insert(role));
        }
        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", role.getId());
        return Result.defaultSuccess(roleMapper.update(role, updateWrapper));
    }

    @Override
    public Result delete(Long id) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getId, id);
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(id);
        roleMapper.deleteRoleMenu(roleMenu);
        return Result.defaultSuccess(roleMapper.delete(queryWrapper));
    }

    @Override
    public Result getRoleMenu(Long id) {
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId(id);
        List<Integer> menu = roleMapper.getMenuByRoleId(roleMenu);
        return Result.defaultSuccess(menu);
    }

    @Override
    public Result insertRoleMenu(RoleMenuDto dto) {
        boolean flag = true;
        RoleMenu roleMenu1 = new RoleMenu();
        roleMenu1.setRoleId(dto.getRoleId());
        int deleted = roleMapper.deleteRoleMenu(roleMenu1);
        List<Long> newList = CollUtil.newArrayList(dto.menuList);
        if (deleted > 0) {
            for (Long id : dto.getMenuList()) {
                LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(Menu::getId,id);
                Menu menu = menuMapper.selectOne(queryWrapper);
                if(menu.getParentId() != 0 && !newList.contains(menu.getParentId())){
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setRoleId(dto.roleId);
                    roleMenu.setMenuId(menu.getParentId());
                    roleMapper.insertRoleMenu(roleMenu);
                    newList.add(menu.getParentId());
                }
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setMenuId(id);
                roleMenu.setRoleId(dto.roleId);
                int cnt = roleMapper.insertRoleMenu(roleMenu);
                if (cnt == 0) {
                    flag = false;
                }
            }
            if (flag) {
                return Result.defaultSuccess(flag);
            }
        }
        return Result.failure(405, "绑定失败");
    }

}